<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>登陆</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
<style type="text/css">
	#code_img{
		position: absolute;
	    top: 52.5%;
	    left: 54%;
	}
	#verify{
		width: 130px;
	    text-align: center;
	    margin: 0px 49% 10px auto;
	}
</style>
</head>
<body>
<div class="htmleaf-container">
	<div class="wrapper">
		<div class="container">
			<h1>Welcome</h1>
			<form class="form" action="" method="post">
				<input type="text" placeholder="账号" name="account" id="account">
				<input type="password" id="password" name="password" placeholder="密码">
				<p>
					<input type="text" class="topAlign" id="verify" name="verify" placeholder="验证码" required>
			        <canvas width="100" height="40" id="verifyCanvas"></canvas>
			        <img id="code_img">
		        </p>
				<button type="submit" id="login-button">登陆</button>
				<button type="button" id="regist-button">注册</button>
			</form>
		</div>
		
		<ul class="bg-bubbles">
			<li style="font-size: 2em">紫</li>
			<li style="font-size: 3em">云</li>
			<li style="font-size: 4em">宝</li>
			<li style="font-size: 2em">宝</li>
			<li style="font-size: 1em">紫</li>
			<li style="font-size: 4em">云</li>
			<li style="font-size: 2em">宝</li>
			<li style="font-size: 4em">宝</li>
			<li style="font-size: 5em">紫</li>
			<li style="font-size: 4em">云</li>
		</ul>
	</div>
</div>

<script src="js/jquery.js" type="text/javascript"></script>
<script src="layer-v3.0.3/layer/layer.js" type="text/javascript"></script>
<script src="js/utils/jsutils.js" type="text/javascript"></script>
<script>
$(function(){
	var rand = new Array();
	$('#login-button').click(function (event) {
		event.preventDefault();
		var msg = checkLogin();
		if(msg != null){
			layer.msg(msg);
			return;
		}
		if(rand.length>0){
			var yzm = "";
			$.each(rand,function(i,v){
				yzm+=v;
			})
			var syzm = $("#verify").val();
			if(yzm.toLowerCase() != syzm.toLowerCase()){
				layer.msg("验证码错误");
				return;
			}
		}else{
			return;
		}
		$('form').fadeOut(500);
		$('.wrapper').addClass('form-success');
		setTimeout(function(){
			JSUtils.loadAjax({
				url:basePath+"/login/loginin.do",
				data:{account:$("#account").val(),password:$("#password").val()},
				success:function(data){
					if(data == 1){
						window.location.href=basePath+"welcome.jsp"
					}else{
						drawCode();
						layer.msg("用户名或密码错误！！兄弟，买挂吗？");
						$('form').fadeIn(1000);
						$('.wrapper').removeClass("form-success");
					}
					
				},
				error:function(){
				}
				
			})
		},500)
	});
	
	function checkLogin(){
		var account = $("#account").val();
		var password = $("#password").val();
		var verify = $("#verify").val();
		if(account == null || account == ""){
			return "账号不允许为空";
		}
		if(password == null || password == ""){
			return "密码不允许为空";
		}
		if(verify == null || verify == ""){
			return "验证码不允许为空";
		}
		return null;
	}
	
	var nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
	             'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
	             'y', 'z'];
    drawCode();
    // 绘制验证码
    function drawCode() {
        var canvas = document.getElementById("verifyCanvas");  //获取HTML端画布
        var context = canvas.getContext("2d");                 //获取画布2D上下文
        context.fillStyle = "cornflowerblue";                  //画布填充色
        context.fillRect(0, 0, canvas.width, canvas.height);   //清空画布
        context.fillStyle = "white";                           //设置字体颜色
        context.font = "25px Arial";                           //设置字体
        rand.splice(0,rand.length);  
        var x = new Array();
        var y = new Array();
        for (var i = 0; i < 5; i++) {
            rand[i] = nums[Math.floor(Math.random() * nums.length)]
            x[i] = i * 16 + 10;
            y[i] = Math.random() * 20 + 20;
            context.fillText(rand[i], x[i], y[i]);
        }
		//alert(rand);
        //画3条随机线
        for (var i = 0; i < 3; i++) {
            drawline(canvas, context);
        }

        // 画30个随机点
        for (var i = 0; i < 30; i++) {
            drawDot(canvas, context);
        }
        convertCanvasToImage(canvas)
    }

    // 随机线
    function drawline(canvas, context) {
        context.moveTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height));             //随机线的起点x坐标是画布x坐标0位置，y坐标是画布高度的随机数
        context.lineTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height));  //随机线的终点x坐标是画布宽度，y坐标是画布高度的随机数
        context.lineWidth = 0.5;                                                  //随机线宽
        context.strokeStyle = 'rgba(50,50,50,0.3)';                               //随机线描边属性
        context.stroke();                                                         //描边，即起点描到终点
    }
    // 随机点(所谓画点其实就是画1px像素的线，方法不再赘述)
    function drawDot(canvas, context) {
        var px = Math.floor(Math.random() * canvas.width);
        var py = Math.floor(Math.random() * canvas.height);
        context.moveTo(px, py);
        context.lineTo(px + 1, py + 1);
        context.lineWidth = 0.2;
        context.stroke();

    }
    // 绘制图片
    function convertCanvasToImage(canvas) {
        document.getElementById("verifyCanvas").style.display = "none";
        var image = document.getElementById("code_img");
        image.src = canvas.toDataURL("image/png");
        return image;
    }

    // 点击图片刷新
    document.getElementById('code_img').onclick = function () {
        $('#verifyCanvas').remove();
        $('#verify').after('<canvas width="100" height="40" id="verifyCanvas"></canvas>')
        drawCode();
    }
})
</script>
</body>
</html>